package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.commen.Result;
import com.itheima.entiry.Category;
import com.itheima.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public Result add(@RequestBody Category category){
        boolean save = categoryService.save(category);
        if (save){
            return new Result(1,"添加成功");
        }return new Result(0,"添加失败");
    }

    @GetMapping("/page")
    public Result getPages(int page,int pageSize){
        Page<Category> page1 = new Page<>(page, pageSize);
        Page<Category> categoryPage = categoryService.page(page1);
        return new Result(1,categoryPage);
    }
//    @GetMapping("/{id}")
//    public Result getById(@PathVariable int id){
//        Category getById = categoryService.getById(id);
//        if (getById==null){
//            return new Result()
//        }
//        return new Result(1,getById);
//    }

    @GetMapping("/list")
    public Result getByType(Integer type){
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(type!=null,Category::getType,type);
        List<Category> list = categoryService.list(qw);
//        Map<String, Object> map = categoryService.getMap(qw);
//        System.out.println(map);
        return new Result(1,list);
    }

    @DeleteMapping
    public Result deleteById(Long id){
        categoryService.remove(id);
        return new Result(1,"删除成功");
    }

    @PutMapping
    public Result update(@RequestBody Category category){
        boolean update = categoryService.updateById(category);
        if (update){
            return new Result(1,"修改成功");
        }return new Result(0,"修改失败");
    }

}
